﻿using System.ComponentModel;
using System.Collections.Generic;
using TreaShares.Backoffice.DataLayer;
using System.Linq;
using System.Data.Linq;
using System;
namespace TreaShares.Backoffice.Controlers
{
    [DataObjectAttribute(true)]
    public class ReportsManager
    {
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<ReportsGetAll> Select()
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                return db.ReportsGetAll().ToList();
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<Report2ClientsResult> SelectReport2Clients(int reportId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                return db.Report2Clients1(reportId).ToList();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public void Insert(string displayName, string reportClassName, string description, bool isPublic)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Reports.InsertOnSubmit(new Report() { DisplayName = displayName, ReportClassName = reportClassName, Description = description, IsPublic = isPublic });
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public void Update(int id, string displayName, string reportClassName, string description, bool isPublic)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Report report = db.Reports.Where(r => r.Id == id).Single();
                report.DisplayName = displayName;
                report.ReportClassName = reportClassName;
                report.Description = description;
                report.IsPublic = isPublic;
                if (isPublic) 
                {
                    db.Report2Clients.DeleteAllOnSubmit(db.Report2Clients.Where(r2c => r2c.ReportId == id));
                }
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void Delete(int id)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Reports.DeleteOnSubmit(db.Reports.Where(r => r.Id == id).Single());
                db.SubmitChanges();
            }
        }
        public void DeleteAllClientReports(int reportId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Report2Clients.DeleteAllOnSubmit(db.Report2Clients.Where(r2c => r2c.ReportId == reportId));
                db.SubmitChanges();
            }
        }


        internal void InsertReport2Clients(Guid clientId, int reportId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Report2Clients.InsertOnSubmit(new Report2Client() { ClientId = clientId, ReportId = reportId });
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public Report SelectReport(int reportId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                return db.Reports.Where(r => r.Id == reportId).Select(r => r).SingleOrDefault();
            }
        }
    }
}